
#include <SPI.h>
#include <LPD8806.h>

  
/*****************************************************************************/
// Kopter light control on LPD8806-based RGB LED Modules in a strip!
/*****************************************************************************/

/*****************************************************************************/
// To be changed according to your setup 
// The LED strips are 40 LEDs per meter but you can extend/cut the strip

#define STRIPES 8                             ///< number of rigger (default: 4)
#define LEDS_PER_STRIPE 20                    ///< Pixels / LEDs per rigger  (default: 10)

#define STRIPSIZE  (STRIPES * LEDS_PER_STRIPE)     ///< Number of all Leds


// Using Hardware SPI
// on Arduino 168/328 thats data = 11, and clock = pin 13
LPD8806 strip = LPD8806(STRIPSIZE, STRIPES);

bool stopSeq = false; // Flag to stop sequence
int time_s;  // current saquence runtime
int ovl_cnt; // interrupt counter



/*************************************************************************//**
 * @brief Doing all initialization here
 *//*************************************************************************/
void setup() 
{
  // Timer settings
  noInterrupts();
  TCCR1A = 0;
  TCCR1B = 0;
  TCNT1  = 0;
  // interrupt every second
  OCR1A = 62500;            // compare match register 16MHz/256/1Hz
  TCCR1B |= (1 << WGM12);   // CTC mode
  TCCR1B |= (1 << CS12);    // 256 prescaler 
  TIMSK1 |= (1 << OCIE1A);  // enable timer compare interrupt
  interrupts();
  
  Serial.begin(9600);
  
   // Random initial value
  randomSeed(analogRead(0));
  
  // Start up the LED strip
  strip.begin();
  // Update the strip, to start they are all 'off'
  strip.show();
  
  Serial.print("RGB Led Stripe Control\n\r" \
               "----------------------\n\r");
               
}



void test()
{
  play();
}

/*****************************************************************//**
 * @brief Main Loop
 *//*****************************************************************/
void loop() 
{
  //test();
  Seq1();
  delay(100);
  Seq2();
  delay(100);	  
  Seq3();
  delay(100);
  Seq4();
  delay(100);
  Seq5();
  delay(100);

}

/* Helper functions *************************************************/

/**
 * @brief Sets duration of a sequence
 * @param sec Duration Time in Seconds
 */
void setTime(uint8_t sec)
{
    stopSeq = false;
    time_s = sec;
    TIMSK1 |= (1<<OCIE1A); // enable interrupt
}



/**
 * @brief Timer compare Interrupt for counting seconds
*/
ISR(TIMER1_COMPA_vect)
{
  // count interrupts and compare with desired time
  if (ovl_cnt++ == time_s) // time is reached
  {
    if (!stopSeq) stopSeq = true; // stop sequence
    TIMSK1 &= ~(1<<OCIE1A); // disable interrupt
    ovl_cnt = 0; // reset
    // prevents overcounting when sequence ist not finished
  }
}





/**
* @brief Only for Checking that all LEDs are working;
*/
void rgb_check()
{
    rgb_t c;
    uint8_t stripe;
    setTime(60);  
	while(!stopSeq)
	{	
// 	        Serial.print("Red\n\r");
		// Red
                c.r=20;
                c.g=0;
                c.b=0;
                for (stripe=1; stripe<=STRIPES; stripe++)
		  strip.setStripeColor(c, stripe);
		strip.show();
		delay(5000);
                clearStripe();
	  	
//                Serial.print("Green\n\r");
		// Green
                c.r=0;
                c.g=20;
                c.b=0;
                for (stripe=1; stripe<=STRIPES; stripe++)
		  strip.setStripeColor(c, stripe);
		strip.show();    
		delay(5000);
		clearStripe();
		
//                Serial.print("Blue\n\r");
                // Blue
                c.r=0;
                c.g=0;
                c.b=20;
                for (stripe=1; stripe<=STRIPES; stripe++)
      	            strip.setStripeColor(c, stripe);
		strip.show();
		delay(5000);
		clearStripe();
	}
}


